var express = require('express');
var app = express();
var bodyParser = require('body-parser');

app.use(express.static(__dirname + '/'));
app.use(bodyParser.urlencoded({extended: true}));


let Web3 = require('web3');
let contract = require('truffle-contract');

//导入合约的ABI文件
let artifacts = require('../build/contracts/patient.json');

let patient = contract(artifacts);

let provider = new Web3.providers.HttpProvider("http://localhost:7545");
patient.setProvider(provider);

patient.defaults({
    from: "0x9Fe10333736242829f7664658E588C694b107CAA",
    gas: 4712388, gasPrice: 100000000000
});
patient.currentProvider.sendAsync = function () {
    return patient.currentProvider.send.apply(patient.currentProvider, arguments);
};

let contract_address = "0xbcf004c560fb626c97a25b6fa19c7b90f99fbb67";
let patientfun = patient.at(contract_address);


app.post('/ajax/patientLogin', function (req, res) {
    var date = req.body.pdate;
    var year = "", month = "", day = "";
    year = date.substr(0, 4);
    month = date.substr(5, 2);
    day = date.substr(8, 2);
    let requrie = {
        patient_name: req.body.pname,
        patient_sex: req.body.psex,
        patient_birth_year: year,
        patient_birth_mouth: month,
        patient_birth_day: day,
        patient_passcard: req.body.pidentity,
        patient_address: req.body.address,
        patient_connection: req.body.connection
    };

    let success;

    patientfun.then(function (contractInstance) {
        contractInstance.init_patient_information(requrie.patient_name, requrie.patient_sex, requrie.patient_birth_year, requrie.patient_birth_mouth, requrie.patient_birth_day, requrie.patient_passcard, requrie.patient_connection, requrie.patient_address).then(function (result) {
            console.log(result);
            success = result;
        })
    }).catch(function(err) {
        console.log(err)
    });

    res.json({
        result: success,
    });
});

app.post('/ajax/doctorLogin', function (req, res) {
    var date = req.body.date;
    var year = "", month = "", day = "";
    year = date.substr(0, 4);
    month = date.substr(5, 2);
    day = date.substr(8, 2);
    let requrie = {
        doctor_name: req.body.title,
        doctor_sex: req.body.sex,
        doctor_birth_year: year,
        doctor_birth_mouth: month,
        doctor_birth_day: day,
        doctor_passcard: req.body.identity,
        doctor_connection: req.body.phone,
        doctor_office_name: req.body.dep,
        doctor_hospital_name: req.body.hospital,
    };

    let success = "false";

    patientfun.then(function (contractInstance) {
        contractInstance.init_doctor_information(requrie.doctor_name, requrie.doctor_sex, requrie.doctor_birth_year, requrie.doctor_birth_mouth, requrie.doctor_birth_day, requrie.doctor_passcard, requrie.doctor_connection, requrie.doctor_office_name, requrie.doctor_hospital_name)
            .then(function (result) {
                console.log(result);
                success = result;
            })
    }).catch(function(err) {
        console.log(err)
    });;

    res.json({
        result: success,
    });
});

app.post('/ajax/get_patient_info', function (req,res) {
    let patient_name;
    let patient_sex;
    let patient_birth_year;
    let patient_birth_mouth;
    let patient_birth_day;
    let patient_passcard;
    let patient_address;
    let patient_connection;

    patientfun.then(function (contractInstance) {
        contractInstance.show_patient_name.call().then(function (result) {
            console.log(result);
            patient_name = result;
        })
    });
    patientfun.then(function (contractInstance) {
        contractInstance.show_patient_sex.call().then(function (result) {
            console.log(result);
            patient_sex = result;
        })
    });
    patientfun.then(function (contractInstance) {
        contractInstance.show_patient_birth_year.call().then(function (result) {
            console.log(result);
            patient_birth_year = result;
        })
    });
    patientfun.then(function (contractInstance) {
        contractInstance.show_patient_birth_mouth.call().then(function (result) {
            console.log(result);
            patient_birth_mouth = result;
        })
    });
    patientfun.then(function (contractInstance) {
        contractInstance.show_patient_birth_day.call().then(function (result) {
            console.log(result);
            patient_birth_day = result;
        })
    });
    patientfun.then(function (contractInstance) {
        contractInstance.show_patient_passcard.call().then(function (result) {
            console.log(result);
            patient_passcard = result;
        })
    });
    patientfun.then(function (contractInstance) {
        contractInstance.show_patient_address.call().then(function (result) {
            console.log(result);
            patient_address = result;
        })
    });
    patientfun.then(function (contractInstance) {
        contractInstance.show_patient_connection.call().then(function (result) {
            console.log(result);
            patient_connection = result;
        })
    });

    setTimeout(function () {

            res.json({
                patient_name: patient_name,
                patient_sex: patient_sex,
                patient_birth_year: patient_birth_year,
                patient_birth_mouth: patient_birth_mouth,
                patient_birth_day: patient_birth_day,
                patient_passcard: patient_passcard,
                patient_address: patient_address,
                patient_connection: patient_connection,
            });
    }, 1 * 1000);

});

app.post('/ajax/get_doctor_info', function (req,res) {
    let doctor_name = "";
    let doctor_sex = "";
    let doctor_birth_year = "";
    let doctor_birth_mouth = "";
    let doctor_birth_day = "";
    let doctor_passcard = "";
    let doctor_connection = "";
    let doctor_hospital_name = "";
    let doctor_office_name = "";
    let count = 0;

    patientfun.then(function (contractInstance) {
        contractInstance.show_doctor_name.call().then(function (result) {
            console.log(result);
            doctor_name = result;
            count++;
        })
    });
    patientfun.then(function (contractInstance) {
        contractInstance.show_doctor_sex.call().then(function (result) {
            console.log(result);
            doctor_sex = result;
            count++;

        })
    });
    patientfun.then(function (contractInstance) {
        contractInstance.show_doctor_birth_year.call().then(function (result) {
            console.log(result);
            doctor_birth_year = result;
            count++;

        })
    });
    patientfun.then(function (contractInstance) {
        contractInstance.show_doctor_birth_mouth.call().then(function (result) {
            console.log(result);
            doctor_birth_mouth = result;
            count++;
        })
    });
    patientfun.then(function (contractInstance) {
        contractInstance.show_doctor_birth_day.call().then(function (result) {
            console.log(result);
            doctor_birth_day = result;
            count++;
        })
    });
    patientfun.then(function (contractInstance) {
        contractInstance.show_doctor_passcard.call().then(function (result) {
            console.log(result);
            doctor_passcard = result;
            count++;
        })
    });
    patientfun.then(function (contractInstance) {
        contractInstance.show_doctor_connection.call().then(function (result) {
            console.log(result);
            count++;
            doctor_connection = result;
        })
    });
    patientfun.then(function (contractInstance) {
        contractInstance.show_doctor_hospital_name.call().then(function (result) {
            console.log(result);
            count++;
            doctor_hospital_name = result;
        })
    });
    patientfun.then(function (contractInstance) {
        contractInstance.show_doctor_office_name.call().then(function (result) {
            console.log(result);
            doctor_office_name = result;
            count++;
        })
    });

    setTimeout(function () {

        res.json({
            data: {
                doctor_name: doctor_name,
                doctor_sex: doctor_sex,
                doctor_birth_year: doctor_birth_year,
                doctor_birth_mouth: doctor_birth_mouth,
                doctor_birth_day: doctor_birth_day,
                doctor_passcard: doctor_passcard,
                doctor_connection: doctor_connection,
                doctor_hospital_name: doctor_hospital_name,
                doctor_office_name: doctor_office_name
            }
        });
    }, 1 * 1000);


});

app.post('/ajax/set_notes', function (req, res) {
    let require = {
        address: req.address,
        result: req.result,
        year: req.year,
        day: req.day,
        mouth: req.month
    };

    let success;
    patientfun.then(function (contractInstance) {
        contractInstance.sendinformation(requrie.address, requrie.result, requrie.year, require.day, require.mouth).then(function (result) {
            console.log(result);
            success = result;
        })
    });

    res.json({
        result: success,
    });


});

app.post('/ajax/get_notes', function (res) {
    let count;
    let notes = [];

    patientfun.deployed().then(function (contractInstance) {
        contractInstance.count().call().then(function (result) {
            console.log(result);
            count = result;
        })
    });
    let i;
    for (i = 0; i < count; i++) {
        let note = {
            year: '',
            mouth: '',
            day: '',
            hospital_name: '',
            doctor_name: '',
            office_name: '',
            result: '',
        };
        patientfun.deployed().then(function (contractInstance) {
            contractInstance.inquiry_year(i).call().then(function (result) {
                console.log(result);
                note.year = result;
            })
        });
        patientfun.deployed().then(function (contractInstance) {
            contractInstance.inquiry_mouth(i).call().then(function (result) {
                console.log(result);
                note.mouth = result;
            })
        });
        patientfun.deployed().then(function (contractInstance) {
            contractInstance.inquiry_day(i).call().then(function (result) {
                console.log(result);
                note.day = result;
            })
        });
        patientfun.deployed().then(function (contractInstance) {
            contractInstance.inquiry_hospital_name(i).call().then(function (result) {
                console.log(result);
                note.hospital_name = result;
            })
        });
        patientfun.deployed().then(function (contractInstance) {
            contractInstance.inquiry_office_name(i).call().then(function (result) {
                console.log(result);
                note.doctor_name = result;
            })
        });
        patientfun.deployed().then(function (contractInstance) {
            contractInstance.inquiry_doctor_name(i).call().then(function (result) {
                console.log(result);
                note.office_name = result;
            })
        });
        patientfun.deployed().then(function (contractInstance) {
            contractInstance.inquiry_result(i).call().then(function (result) {
                console.log(result);
                note.result = result;
            })
        });

        notes.push(note);
    }

    res.json({
        notes: notes
    })
});

app.post('/ajax/test', function (req, res) {
    res.json({
        data: "success"
    })
});

var server = app.listen(8080, "127.0.0.1", function () {
    var host = server.address().address;
    var port = server.address().port;

    console.log('服务器启动 http://%s:%s', host, port);
});

